package com.nq.fund.service.impl;

import com.nq.dao.fund.StFundKlineMapper;
import com.nq.dao.fund.StFundMapper;
import com.nq.dao.fund.StFundServiceChargeDetailsMapper;
import com.nq.dao.fund.StFundServiceChargeMapper;
import com.nq.fund.entity.StFund;
import com.nq.fund.entity.StFundKline;
import com.nq.fund.entity.StFundServiceCharge;
import com.nq.fund.entity.StFundServiceChargeDetails;
import com.nq.fund.service.IStFundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: st_fund
 * @Author: jeecg-boot
 * @Date:   2024-06-03
 * @Version: V1.0
 */
@Service
public class StFundServiceImpl extends ServiceImpl<StFundMapper, StFund> implements IStFundService {

    @Autowired
    private StFundServiceChargeMapper fundServiceChargeMapper;
    @Autowired
    private StFundServiceChargeDetailsMapper fundServiceChargeDetailsMapper;
    @Autowired
    private StFundKlineMapper fundKlineMapper;


    @Override
    public int editFundServiceCharge(StFundServiceCharge serviceCharge) {
        Integer serviceChargeId = serviceCharge.getId();
        if (null == serviceChargeId) {
            fundServiceChargeMapper.insert(serviceCharge);
        }
        else {
            fundServiceChargeMapper.updateById(serviceCharge);
        }
        return 1;
    }

    @Override
    public int editFundServiceChargeDetails(StFundServiceChargeDetails serviceCharge) {

        Integer serviceChargeId = serviceCharge.getId();
        if (null == serviceChargeId) {
            fundServiceChargeDetailsMapper.insert(serviceCharge);
        }
        else {
            fundServiceChargeDetailsMapper.updateById(serviceCharge);
        }

        return 1;
    }

    @Override
    @Transactional
    public int addFund(StFund stFund) {

        BigDecimal currentPrice = stFund.getCurrentPrice();
        // 新增时同步新增当天的K线
        if (null != currentPrice) {
            StFundKline kline = new StFundKline();
            kline.setFundCode(stFund.getFundCode());
            kline.setOpen(currentPrice);
            kline.setClose(currentPrice);
            kline.setHigh(currentPrice);
            kline.setLow(currentPrice);
            kline.setVolume(BigDecimal.ZERO);
            kline.setTurnover(BigDecimal.ZERO);
            kline.setCreateTime(new Date());
            kline.setTimestamp(System.currentTimeMillis() / 1000);
            fundKlineMapper.insert(kline);
        }

        return baseMapper.insert(stFund);
    }
}
